package com.zoe.wan.android.example.common.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ItemKnowledgeListBinding
import com.zoe.wan.android.example.repository.data.KnowledgeListDataItem
import com.zoe.wan.base.adapter.BaseAdapter
import com.zoe.wan.base.adapter.BaseViewHolder
import java.lang.StringBuilder


/**
 *  知识体系适配器
 */
class KnowledgeListAdapter : BaseAdapter<KnowledgeListDataItem?,KnowledgeListAdapter.KnowledgeItemViewHolder>() {

    class KnowledgeItemViewHolder(itemBinding: ItemKnowledgeListBinding) :
        BaseViewHolder<ItemKnowledgeListBinding>(itemBinding)

    override fun getViewHolder(parent: ViewGroup, viewType: Int): KnowledgeItemViewHolder {
        return KnowledgeItemViewHolder(getBinding(parent,R.layout.item_knowledge_list))
    }

    override fun bindHolder(holder: KnowledgeItemViewHolder, position: Int) {
        val item = getDataList()?.get(position)
        holder.binding.item = item
        val sb = StringBuilder()
        // 子标题
        item?.children?.take(10)?.forEach { child ->
            sb.append("${child?.name}  ")
        }
        if ((item?.children?.size ?: 0) > 10) {
            sb.append("......")
        }
        holder.binding.knowledgeItemSubTitle.text = sb.toString()
    }

}